home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pavt150.zip / CAPI.ZIP / JAMSYS.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  24KB  |  882 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  C API
  5. **
  6. **  Written by Mats Wallin
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jamsys.c (JAMmb)
  11. **
  12. **  Platform/compiler dependant functions
  13. **
  14. **  See jamsys.h for information about assumptions made about compilers
  15. **  and platforms
  16. **
  17. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  18. **  Mats Wallin. ALL RIGHTS RESERVED.
  19. **
  20. **  93-06-28    MW
  21. **  Initial coding
  22. */
  23. #define JAMCAPI 1
  24.  
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #if defined(__MSDOS__) || defined(_WINDOWS)
  31.     #include <fcntl.h>
  32.     #include <io.h>
  33.     #include <share.h>
  34.     #include <dos.h>
  35. #endif
  36.  
  37. #if defined(__TURBOC__) || defined(__BORLANDC__) || \
  38.         defined(__ZTC__) || defined(_MSC_VER) || defined(_QC)
  39.     #include <sys\types.h>
  40.     #include <sys\stat.h>
  41. #endif
  42.  
  43. #if defined(__ZTC__) || defined(_MSC_VER) || defined(_QC)
  44.     #include <sys\locking.h>
  45. #endif
  46.  
  47. #if defined(__TSC__)
  48.     #include <stat.h>
  49.     #include <locking.h>
  50. #endif
  51.  
  52. #if defined(__sparc__)
  53.     #include <fcntl.h>
  54.     #include <sys/file.h>
  55.     #include <memory.h>
  56.     #include <sys/time.h>
  57. #endif
  58.  
  59. #if defined(__50SERIES)
  60.     #define unlink delete
  61. #endif
  62.  
  63. #if defined(_MSC_VER)
  64.     #define locking _locking
  65.     #define unlink  _unlink
  66. #endif
  67.  
  68. #include "jammb.h"
  69.  
  70. /*
  71. **  Number of days since January 1, the 1st of each month
  72. */
  73. static int _mdays [13] =
  74.             {
  75. /* Jan */   0,
  76. /* Feb */   31,
  77. /* Mar */   31+28,
  78. /* Apr */   31+28+31,
  79. /* May */   31+28+31+30,
  80. /* Jun */   31+28+31+30+31,
  81. /* Jul */   31+28+31+30+31+30,
  82. /* Aug */   31+28+31+30+31+30+31,
  83. /* Sep */   31+28+31+30+31+30+31+31,
  84. /* Oct */   31+28+31+30+31+30+31+31+30,
  85. /* Nov */   31+28+31+30+31+30+31+31+30+31,
  86. /* Dec */   31+28+31+30+31+30+31+31+30+31+30,
  87. /* Jan */   31+28+31+30+31+30+31+31+30+31+30+31
  88.             };
  89.  
  90. /*
  91. **  JAMsysInitApiRec
  92. **
  93. **  Initializes the JAMAPIREC structure with all needed information
  94. **
  95. **      Parameters:     JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  96. **                      CHAR8ptr pFile      - Name of JAM messagebase
  97. **                      UINT32 Size         - Size of workbuffer to allocate
  98. **
  99. **         Returns:     0                   - Failure to allocate workbuffer
  100. **                      1                   - Success
  101. */
  102. int _JAMPROC JAMsysInitApiRec(JAMAPIRECptr apirec, CHAR8ptr pFile, UINT32 Size)
  103. {
  104.     memset(apirec, 0, sizeof(JAMAPIREC));
  105.  
  106. #if defined(_WINDOWS)
  107.         {
  108.         HGLOBAL   hMem;
  109.  
  110.         if ((hMem = GlobalAlloc (GPTR, Size)) != (HGLOBAL) NULL)
  111.             if ((apirec->WorkBuf = GlobalLock (hMem)) == NULL)
  112.                 GlobalFree (hMem);
  113.         }
  114. #else
  115.     apirec->WorkBuf=malloc((unsigned int)Size);
  116. #endif
  117.     if (apirec->WorkBuf==NULL)
  118.         return (0);
  119.  
  120.     strcpy(apirec->BaseName, pFile);
  121.     apirec->WorkLen=Size;
  122.  
  123.     apirec->HdrHandle=
  124.         apirec->TxtHandle=
  125.             apirec->IdxHandle=
  126.                 apirec->LrdHandle=-1;
  127.  
  128.     apirec->CreateFunc = JAMsysCreate;
  129.     apirec->OpenFunc   = JAMsysOpen;
  130.     apirec->CloseFunc  = JAMsysClose;
  131.     apirec->ReadFunc   = JAMsysRead;
  132.     apirec->WriteFunc  = JAMsysWrite;
  133.     apirec->SeekFunc   = JAMsysSeek;
  134.     apirec->LockFunc   = JAMsysLock;
  135.     apirec->UnlinkFunc = JAMsysUnlink;
  136.  
  137. #if defined(_WINDOWS)
  138.         {
  139.         UINT16  WinVer;
  140.  
  141.         WinVer = LOWORD (GetVersion ());
  142.         if (LOBYTE (WinVer) > 3 ||
  143.             (LOBYTE (WinVer) == 3 && HIBYTE (WinVer) >= 10))
  144.             {
  145.             apirec->ReadFunc  = JAMsysReadHuge;
  146.             apirec->WriteFunc = JAMsysWriteHuge;
  147.             }
  148.         }
  149. #endif
  150.  
  151.     return (1);
  152. }
  153.  
  154. /*
  155. **  JAMsysDeinitApiRec
  156. **
  157. **  Deinitializes the JAMAPIREC structure. If the message base still is
  158. **  opened, it is closed
  159. **
  160. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  161. **
  162. **         Returns:   0                   - Failure to allocate workbuffer
  163. **                    1                   - Success
  164. */
  165. int _JAMPROC JAMsysDeinitApiRec(JAMAPIRECptr apirec)
  166. {
  167.     /*This will release any locks we might has as well*/
  168.     if (apirec->isOpen)
  169.         JAMmbClose(apirec);
  170.  
  171.     if (apirec->WorkBuf!=NULL)
  172.         {
  173. #if defined(_WINDOWS)
  174.         HGLOBAL     hMem;
  175.  
  176.         hMem = (HGLOBAL) LOWORD (GlobalHandle (SELECTOROF (apirec->WorkBuf)));
  177.         GlobalUnlock (hMem);
  178.         GlobalFree (hMem);
  179. #else
  180.         free(apirec->WorkBuf);
  181. #endif
  182.         }
  183.  
  184.     apirec->WorkBuf=NULL;
  185.     apirec->WorkLen=0L;
  186.  
  187.     apirec->BaseName[0]='\0';
  188.  
  189.     return (1);
  190. }
  191.  
  192. /*
  193. **  JAMsysClose
  194. **
  195. **  Closes the specified filehandle
  196. **
  197. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  198. **                    FHANDLE fh          - Filehandle
  199. **
  200. **         Returns:   0 on success, or -1 in the case of an error
  201. */
  202. int _JAMPROC JAMsysClose(JAMAPIRECptr apirec, FHANDLE fh)
  203. {
  204. #if defined(_WINDOWS)
  205.     if (_lclose(fh)==HFILE_ERROR)
  206. #elif defined(__MSDOS__) && \
  207.         (defined(__TURBOC__) || defined(__BORLANDC__) || defined(__TSC__))
  208.     if (_close(fh)<0)
  209. #else
  210.     if (close(fh)<0)
  211. #endif
  212.         {
  213.         if (apirec)
  214.             apirec->Errno=errno;
  215.         return (-1);
  216.         }
  217.  
  218.     return (0);
  219. }
  220.  
  221. /*
  222. **  JAMsysCreate
  223. **
  224. **  Creates the specified file, closes it, and then reopens it in the
  225. **  correct mode.
  226. **
  227. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  228. **                    CHAR8ptr pFileName  - Name of file to create
  229. **
  230. **         Returns:   The filehandle when successful, -1 if it fails
  231. */
  232. FHANDLE _JAMPROC JAMsysCreate(JAMAPIRECptr apirec, CHAR8ptr pFileName)
  233. {
  234.     FHANDLE fh;
  235.  
  236. #if defined(__50SERIES)
  237.     fh=creat(pFileName, 2);
  238. #elif defined(_WINDOWS)
  239.     fh=_lcreat(pFileName, 0);
  240. #elif defined(__MSDOS__) && \
  241.         (defined(__TURBOC__) || defined(__BORLANDC__) || defined(__TSC__))
  242.     fh=_creat(pFileName, 0);
  243. #else
  244.     fh=creat(pFileName, S_IREAD|S_IWRITE);
  245. #endif
  246.     if (fh<0)
  247.         {
  248.         if (apirec)
  249.             apirec->Errno = errno;
  250.         return (-1);
  251.         }
  252.  
  253.     if (JAMsysClose(apirec, fh)<0)
  254.         return (-1);
  255.  
  256. #if defined(__sparc__) || defined(__50SERIES)
  257.     return (fh);
  258. #else
  259.     return (JAMsysOpen(apirec, pFileName));
  260. #endif
  261. }
  262.  
  263. /*
  264. **  JAMsysLock
  265. **
  266. **  Sets or resets the lock on the JAM messagebase. This is done with
  267. **  a lock on the first byte in the header file
  268. **
  269. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  270. **                    int DoLock          - 1 if messagebase should be locked
  271. **                                        - 0 to unlock messagebase
  272. **
  273. **         Returns:   0 on success, and -1 on failure
  274. */
  275. int _JAMPROC JAMsysLock(JAMAPIRECptr apirec, int DoLock)
  276. {
  277. #if defined(__MSDOS__) || defined(_WINDOWS)
  278.     #if defined(__TURBOC__) || defined(__BORLANDC__)
  279.         int stat;
  280.  
  281.         if (DoLock)
  282.             stat=lock(apirec->HdrHandle, 0L, 1L);
  283.         else 
  284.             stat=unlock(apirec->HdrHandle, 0L, 1L);
  285.  
  286.         if (stat==-1)
  287.             apirec->Errno=errno;
  288.  
  289.         return (stat);
  290.     #elif defined(__TSC__) || defined(__ZTC__) || \
  291.               defined(_MSC_VER) || defined(_QC)
  292.         int   stat;
  293.         INT32 OldPos;
  294.  
  295.         /*Get old position*/
  296.         OldPos=JAMsysSeek(apirec, apirec->HdrHandle, JAMSEEK_CUR, 0L);
  297.         if (OldPos<0L)
  298.             return (-1);
  299.  
  300.         /*Move to beginning of file*/
  301.         if (JAMsysSeek(apirec, apirec->HdrHandle, JAMSEEK_SET, 0L)<0L)
  302.             return (-1);
  303.  
  304.         stat=locking(apirec->HdrHandle, DoLock ? LK_NBLCK:LK_UNLCK, 1L);
  305.  
  306.         /*Restore position*/
  307.         JAMsysSeek(apirec, apirec->HdrHandle, JAMSEEK_SET, OldPos);
  308.  
  309.         return (stat);
  310.     #else
  311.         #error Unsupported compiler
  312.     #endif
  313. #elif defined(__sparc__)
  314.     DoLock = DoLock ? LOCK_EX : LOCK_UN;
  315.     if (flock(apirec->HdrHandle, DoLock) == -1)
  316.         {
  317.         apirec->Errno = errno;
  318.         return (-1);
  319.         }
  320.     return (0);
  321. #elif defined(__50SERIES)
  322.     return (0);
  323. #else
  324.     #error Unsupported platform
  325. #endif
  326. }
  327.  
  328. /*
  329. **  JAMsysOpen
  330. **
  331. **  Opens the specified file in ReadWrite/DenyNone mode
  332. **
  333. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  334. **                    CHAR8ptr pFileName  - Name of file to open
  335. **
  336. **         Returns:   The filehandle when successful, -1 if it fails
  337. */
  338. FHANDLE _JAMPROC JAMsysOpen(JAMAPIRECptr apirec, CHAR8ptr pFileName)
  339. {
  340.     FHANDLE fh;
  341.  
  342.     fh=JAMsysSopen (apirec, pFileName, JAMO_RDWR, JAMSH_DENYNO);
  343.     return (fh);
  344. }
  345.  
  346. /*
  347. **  JAMsysRead
  348. **
  349. **  Reads the specified number of bytes from the specified filehandle
  350. **
  351. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  352. **                    FHANDLE fh          - Filehandle of file to read from
  353. **                    VOIDptr pBuf        - Buffer where read data are stored
  354. **                    INT32               - Number of bytes to read
  355. **
  356. **         Returns: The number of bytes read, or -1 in the case of error
  357. **
  358. **  NOTE! In small and medium memory model, the maximum size that can
  359. **        be read is 0xFF00 bytes
  360. */
  361. INT32 _JAMPROC JAMsysRead(JAMAPIRECptr apirec, FHANDLE fh, VOIDptr pBuf, INT32 Len)
  362. {
  363.     INT32       TotalRead = 0;
  364.  
  365. #if defined(__MSDOS__) || defined(_WINDOWS)
  366.     UCHAR8    * Ptr       = pBuf;
  367.  
  368.     while(Len)
  369.         {
  370.         UINT16  ToRead,
  371.                 BytesRead;
  372.  
  373.         ToRead = (UINT16) ((Len > 0xFF00L) ? 0xFF00u : (UINT16) Len);
  374.  
  375. #if defined(_WINDOWS)
  376.         if((BytesRead = (UINT16) _lread(fh, Ptr, ToRead)) == (UINT16) HFILE_ERROR)
  377. #elif defined(__MSDOS__) && \
  378.           (defined(__TURBOC__) || defined(__BORLANDC__) || defined(__TSC__))
  379.         if((BytesRead = (UINT16) _read(fh, Ptr, ToRead)) == (UINT16) -1)
  380. #else
  381.         if((BytesRead = (UINT16) read(fh, Ptr, ToRead)) == (UINT16) -1)
  382. #endif
  383.             {
  384.             if (apirec)
  385.                 apirec->Errno = errno;
  386.             return(-1);
  387.             }
  388.  
  389.         TotalRead += BytesRead;
  390.         if(BytesRead != ToRead)
  391.             return(TotalRead);
  392.  
  393. #if defined(__SMALL__) || defined(__MEDIUM__)
  394.         return(TotalRead);
  395. #else
  396.         Len -= BytesRead;
  397.         Ptr = JAMsysAddPtr(Ptr, (INT32) BytesRead);
  398. #endif
  399.         }
  400.  
  401.     return(TotalRead);
  402.  
  403. #else   /* #if defined(__MSDOS__) || defined(_WINDOWS) */
  404.  
  405.     if((TotalRead = read(fh, pBuf, Len )) == -1)
  406.         {
  407.         if (apirec)
  408.             apirec->Errno = errno;
  409.         }
  410.  
  411.     return(TotalRead);
  412.  
  413. #endif  /* #ifdef __MSDOS__ */
  414. }
  415.  
  416. #if defined(_WINDOWS)
  417. /*
  418. **  JAMsysReadHuge
  419. **
  420. **  Reads the specified number of bytes from the specified filehandle
  421. **
  422. **  _WINDOWS specific function
  423. **
  424. **  This function uses the Windows 3.1 function _hread to be able to
  425. **  read blocks larger then 64 Kb.
  426. **
  427. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  428. **                    FHANDLE fh          - Filehandle of file to read from
  429. **                    VOIDptr pBuf        - Buffer where read data are stored
  430. **                    INT32               - Number of bytes to read
  431. **
  432. **         Returns: The number of bytes read, or -1 in the case of error
  433. */
  434. INT32 _JAMPROC JAMsysReadHuge(JAMAPIRECptr apirec, FHANDLE fh, VOIDptr pBuf, INT32 Len)
  435. {
  436.     INT32       TotalRead;
  437.  
  438.     if((TotalRead = _hread(fh, (void _HUGE *) pBuf, Len )) == -1)
  439.         {
  440.         if (apirec)
  441.             apirec->Errno = errno;
  442.         }
  443.  
  444.     return(TotalRead);
  445. }
  446. #endif /* #if defined(_WINDOWS) */
  447.  
  448. /*
  449. **  JAMsysSeek
  450. **
  451. **  Move the file pointer for the filehandle to the specified location.
  452. **
  453. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  454. **                    FHANDLE fh          - Filehandle
  455. **                    int FromWhere       - From where should the seek start
  456. **                    INT32 Offset        - Which position to seek to
  457. **
  458. **         Returns:   The new offset of the file pointer, from the beginning
  459. **                    of the file, or -1L in the case of error
  460. */
  461. INT32 _JAMPROC JAMsysSeek(JAMAPIRECptr apirec, FHANDLE fh, int FromWhere, INT32 Offset)
  462. {
  463.     INT32   NewOffset;
  464.  
  465. #if defined(_WINDOWS)
  466.     if((NewOffset = _llseek(fh, Offset, FromWhere)) == HFILE_ERROR)
  467. #else
  468.     if((NewOffset = lseek(fh, Offset, FromWhere)) == -1L)
  469. #endif
  470.         {
  471.         if (apirec)
  472.             apirec->Errno = errno;
  473.         }
  474.  
  475.     return(NewOffset);
  476. }
  477.  
  478. /*
  479. **  JAMsysSopen
  480. **
  481. **  Opens the specified file in the specified access and sharing mode
  482. **
  483. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  484. **                    CHAR8ptr pFileName  - Name of file to open
  485. **                    int AccessMode      - AccessMode
  486. **                    int ShareMode       - ShareMode
  487. **
  488. **         Returns:   The filehandle when successful, -1 if it fails
  489. */
  490. FHANDLE _JAMPROC JAMsysSopen(JAMAPIRECptr apirec, CHAR8ptr pFileName, int AccessMode, int ShareMode)
  491. {
  492.     FHANDLE fh;
  493.  
  494. #if defined(__sparc__) || defined(__50SERIES)
  495.     fh=open(pFileName, AccessMode);
  496. #elif defined(_WINDOWS)
  497.     fh=_lopen(pFileName, AccessMode|ShareMode);
  498. #elif defined(__MSDOS__) && \
  499.         (defined(__TURBOC__) || defined(__BORLANDC__) || defined(__TSC__))
  500.     fh=_open(pFileName, AccessMode|ShareMode);
  501. #else
  502.     fh=sopen(pFileName, AccessMode, ShareMode, S_IREAD|S_IWRITE);
  503. #endif
  504.     if (fh<0)
  505.         {
  506.         if (apirec)
  507.             apirec->Errno=errno;
  508.         }
  509.  
  510.     return (fh);
  511. }
  512.  
  513. /*
  514. **  JAMsysUnlink
  515. **
  516. **  Deletes the specified file
  517. **
  518. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  519. **                    CHAR8PTR pFileName   - Name of file to delete
  520. **
  521. **         Returns:   0 if successful, -1 in the case of error
  522. */
  523. int _JAMPROC JAMsysUnlink(JAMAPIRECptr apirec, CHAR8ptr pFileName)
  524. {
  525.     if(unlink(pFileName) == -1)
  526.         {
  527.         if (apirec)
  528.             apirec->Errno = errno;
  529.         return(-1);
  530.         }
  531.  
  532.     return(0);
  533. }
  534.  
  535. /*
  536. **  JAMsysWrite
  537. **
  538. **  Writes the specified number of bytes from the specified filehandle
  539. **
  540. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  541. **                    FHANDLE fh          - Filehandle of file to write to
  542. **                    VOIDptr pBuf        - Buffer with data to write
  543. **                    INT32 Len           - Number of bytes to write
  544. **
  545. **         Returns:   The number of bytes written, or -1 in the case of error
  546. **
  547. **  NOTE! In small and medium memory model, the maximum size that can
  548. **        be written is 0xFF00 bytes
  549. */
  550. INT32 _JAMPROC JAMsysWrite(JAMAPIRECptr apirec, FHANDLE fh, VOIDptr pBuf, INT32 Len)
  551. {
  552.     INT32       TotalWrit = 0L;
  553.  
  554. #if defined(__MSDOS__) || defined(_WINDOWS)
  555.     UCHAR8    * Ptr       = pBuf;
  556.  
  557.     while(Len)
  558.         {
  559.         UINT16  ToWrit,
  560.                 BytesWrit;
  561.  
  562.         ToWrit = (UINT16) ((Len > 0xFF00L) ? 0xFF00u : (UINT16) Len);
  563.  
  564. #if defined(_WINDOWS)
  565.         if((BytesWrit = (UINT16) _lwrite(fh, Ptr, ToWrit)) == (UINT16) HFILE_ERROR)
  566. #elif defined(__MSDOS__) && \
  567.           (defined(__TURBOC__) || defined(__BORLANDC__) || defined(__TSC__))
  568.         if((BytesWrit = (UINT16) _write(fh, Ptr, ToWrit)) == (UINT16) -1)
  569. #else
  570.         if((BytesWrit = (UINT16) write(fh, Ptr, ToWrit)) == (UINT16) -1)
  571. #endif
  572.             {
  573.             if (apirec)
  574.                 apirec->Errno = errno;
  575.             return(-1L);
  576.             }
  577.  
  578.         TotalWrit += BytesWrit;
  579.         if(BytesWrit != ToWrit)
  580.             return(TotalWrit);
  581.  
  582. #if defined(__SMALL__) || defined(__MEDIUM__)
  583.         return(TotalWrit);
  584. #else
  585.         Len -= BytesWrit;
  586.         Ptr = JAMsysAddPtr(Ptr, (INT32) BytesWrit);
  587. #endif
  588.         }
  589.  
  590.     return(TotalWrit);
  591.  
  592. #else   /* #if defined(__MSDOS__) || defined(_WINDOWS) */
  593.  
  594.     if((TotalWrit = write(fh, pBuf, Len)) == -1)
  595.         {
  596.         if (apirec)
  597.             apirec->Errno = errno;
  598.         }
  599.   
  600.     return(TotalWrit);
  601.  
  602. #endif  /* #ifdef __MSDOS__ */
  603. }
  604.  
  605. #if defined(_WINDOWS)
  606. /*
  607. **  JAMsysWriteHuge
  608. **
  609. **  Writes the specified number of bytes from the specified filehandle
  610. **
  611. **  _WINDOWS specific function
  612. **
  613. **  This function uses the Windows 3.1 function _hwrite to be able to
  614. **  write blocks larger then 64 Kb.
  615. **
  616. **      Parameters:   JAMAPIRECptr apirec - Ptr to JAMAPIREC to initiate
  617. **                    FHANDLE fh          - Filehandle of file to write to
  618. **                    VOIDptr pBuf        - Buffer with data to write
  619. **                    INT32 Len           - Number of bytes to write
  620. **
  621. **         Returns:   The number of bytes written, or -1 in the case of error
  622. */
  623. INT32 _JAMPROC JAMsysWriteHuge(JAMAPIRECptr apirec, FHANDLE fh, VOIDptr pBuf, INT32 Len)
  624. {
  625.     INT32       TotalWrit;
  626.  
  627.     if((TotalWrit = _hwrite(fh, (void _HUGE *) pBuf, Len)) == -1)
  628.         {
  629.         if (apirec)
  630.             apirec->Errno = errno;
  631.         }
  632.   
  633.     return(TotalWrit);
  634.  
  635. }
  636. #endif /* #if defined(_WINDOWS) */
  637.  
  638. #if defined(__MSDOS__)
  639. /*
  640. **  JAMsysAddPtr            [ When compiling in a far  data model ]
  641. **  JAMsysAddFarPtr         [ When compiling in a near data model ]
  642. **
  643. **  __MSDOS__ specific function
  644. **
  645. **  Adds a long offset to a far (huge) pointer. Supports situations
  646. **  when more then 0xFFFF bytes are added to the pointer.
  647. **  The resulting pointer is "normalized", e.g. the offset part will
  648. **  have a value between 0x0000 and 0x000F
  649. **
  650. **      Parameters:   void _JAMFAR * Ptr  - Ptr to increase
  651. **                    INT32 Offset        - Offset to add to pointer
  652. **
  653. **         Returns:   The new pointer after the offset has been added
  654. */
  655. #if defined(__SMALL__) || defined(__MEDIUM__)
  656. void _JAMFAR * _JAMPROC JAMsysAddFarPtr(void _JAMFAR *Ptr, INT32 Offset)
  657. #else
  658. void _JAMFAR * _JAMPROC JAMsysAddPtr(void _JAMFAR *Ptr, INT32 Offset)
  659. #endif
  660. {
  661.     INT32   lPtr;
  662.  
  663.     lPtr = ((INT32) *((UINT16 *) &Ptr)) +
  664.            ((INT32) (*((UINT16 *) &Ptr + 1)) << 4) +
  665.            Offset;
  666.  
  667.     return((void _JAMFAR *) (((INT32) ((UINT16) lPtr & 0x000F)) | ((lPtr & 0x000FFFF0uL) << 12)));
  668. }
  669. #endif  /* #if defined(__MSDOS__) */
  670.  
  671. /*
  672. **  JAMsysTime
  673. **
  674. **  Calculates the number of seconds that has passed from January 1, 1970
  675. **  until the current date and time.
  676. **
  677. **      Parameters:   UINT32ptr pTime     - Ptr to buffer where the number
  678. **                                          number of seconds will be stored
  679. **
  680. **         Returns:   Number of seconds since 1970 to specified date/time
  681. */
  682. UINT32 _JAMPROC JAMsysTime(UINT32ptr pTime)
  683. {
  684. #if defined(__MSDOS__) || defined(_WINDOWS)
  685.     #if defined(__ZTC__) || defined(_MSC_VER) || defined(_QC)
  686.         #if defined(__ZTC__)
  687.             struct dos_date_t   d;
  688.             struct dos_time_t   t;
  689.         #elif defined (_MSC_VER) || defined(_QC)
  690.             struct _dosdate_t   d;
  691.             struct _dostime_t   t;
  692.         #endif
  693.  
  694.         struct JAMtm  m;
  695.         UINT32        ti;
  696.  
  697.         #if defined(__ZTC__)
  698.             dos_getdate(&d);
  699.             dos_gettime(&t);
  700.         #elif defined (_MSC_VER) || defined(_QC)
  701.             _dos_getdate(&d);
  702.             _dos_gettime(&t);
  703.         #endif
  704.  
  705.         m.tm_year = d.year - 1900;
  706.         m.tm_mon  = d.month - 1;
  707.         m.tm_mday = d.day;
  708.         m.tm_hour = t.hour;
  709.         m.tm_min  = t.minute;
  710.         m.tm_sec  = t.second;
  711.     #elif defined(__TURBOC__) || defined(__BORLANDC__) || defined(__TSC__)
  712.         struct date   d;
  713.         struct time   t;
  714.         struct JAMtm  m;
  715.         UINT32        ti;
  716.  
  717.         getdate(&d);
  718.         gettime(&t);
  719.  
  720.         m.tm_year = d.da_year - 1900;
  721.         m.tm_mon  = d.da_mon - 1;
  722.         m.tm_mday = d.da_day;
  723.         m.tm_hour = t.ti_hour;
  724.         m.tm_min  = t.ti_min;
  725.         m.tm_sec  = t.ti_sec;
  726.     #endif
  727. #elif defined(__OS2__)
  728.           DATETIME      DT;
  729.     struct JAMtm  m;
  730.     UINT32        ti;
  731.  
  732.     DosGetDateTime(&DT);
  733.  
  734.     m.tm_year = DT.year - 1900;
  735.     m.tm_mon  = DT.month - 1;
  736.     m.tm_day  = DT.day;
  737.     m.tm_hour = DT.hours;
  738.     m.tm_min  = DT.minutes;
  739.     m.tm_sec  = DT.seconds;
  740. #endif
  741.  
  742. #if defined(__MSDOS__) || defined(_WINDOWS) || defined(__OS2__)
  743.     ti = JAMsysMkTime(&m);
  744.     if(pTime)
  745.         *pTime = ti;
  746.  
  747.     return(ti);
  748. #else
  749.     return(time(pTime));
  750. #endif
  751. }
  752.  
  753. /*
  754. **  JAMsysMkTime
  755. **
  756. **  Calculates the number of seconds that has passed from January 1, 1970
  757. **  until the specified date and time.
  758. **
  759. **      Parameters:   JAMTMptr pTm        - Ptr to structure containing time
  760. **
  761. **         Returns:   Number of seconds since 1970 to specified date/time
  762. */
  763. UINT32 _JAMPROC JAMsysMkTime(JAMTMptr pTm)
  764. {
  765.     UINT32  Days;
  766.     int     Years;
  767.  
  768.     /*Get number of years since 1970*/
  769.     Years = pTm->tm_year - 70;
  770.  
  771.     /*Calculate number of days during these years,*/
  772.     /*including extra days for leap years         */
  773.     Days = Years * 365 + ((Years + 1) / 4);
  774.  
  775.     /*Add the number of days during this year*/
  776.     Days += _mdays [pTm->tm_mon] + pTm->tm_mday - 1;
  777.     if((pTm->tm_year & 3) == 0 && pTm->tm_mon > 1)
  778.         Days++;
  779.  
  780.     /*Convert to seconds, and add the number of seconds this day*/
  781.     return(((UINT32) Days * 86400L) + ((UINT32) pTm->tm_hour * 3600L) +
  782.            ((UINT32) pTm->tm_min * 60L) + (UINT32) pTm->tm_sec);
  783. }
  784.  
  785. /*
  786. **  JAMsysLocalTime
  787. **
  788. **  Converts the specified number of seconds since January 1, 1970, to
  789. **  the corresponding date and time.
  790. **
  791. **      Parameters:   UINT32ptr pt        - Number of seconds since Jan 1, 1970
  792. **
  793. **         Returns:   Ptr to struct JAMtm area containing date and time
  794. */
  795. JAMTMptr _JAMPROC JAMsysLocalTime(UINT32ptr pt)
  796. {
  797.     static struct JAMtm   m;
  798.     INT32                 t = *pt;
  799.     int                   LeapDay;
  800.  
  801.     m.tm_sec  = (int) (t % 60); t /= 60;
  802.     m.tm_min  = (int) (t % 60); t /= 60;
  803.     m.tm_hour = (int) (t % 24); t /= 24;
  804.     m.tm_wday = (int) ((t + 4) % 7);
  805.  
  806.     m.tm_year = (int) (t / 365 + 1);
  807.     do
  808.         {
  809.         m.tm_year--;
  810.         m.tm_yday = (int) (t - m.tm_year * 365 - ((m.tm_year + 1) / 4));
  811.         }
  812.     while(m.tm_yday < 0);
  813.     m.tm_year += 70;
  814.  
  815.     LeapDay = ((m.tm_year & 3) == 0 && m.tm_yday >= _mdays [2]);
  816.  
  817.     for(m.tm_mon = m.tm_mday = 0; m.tm_mday == 0; m.tm_mon++)
  818.         if(m.tm_yday < _mdays [m.tm_mon + 1] + LeapDay)
  819.             m.tm_mday = m.tm_yday + 1 - (_mdays [m.tm_mon] + (m.tm_mon != 1 ? LeapDay : 0));
  820.  
  821.     m.tm_mon--;
  822.  
  823.     m.tm_isdst = -1;
  824.  
  825.     return(&m);
  826. }
  827.  
  828. #if defined(_WINDOWS)
  829. /*
  830. **  LibMain
  831. **
  832. **  Called by the Windows DLL startup code
  833. **
  834. **      Parameters:   HANDLE hInst      - DLL instance handle
  835. **                    WORD wDataSeg     - ?
  836. **                    WORD cbHeapSize   - Size of the heap
  837. **                    LPSTR lpszCmdLine - Command line used when loading DLL
  838. **
  839. **         Returns:   TRUE to continue, FALSE to abort load of DLL
  840. */
  841. #if defined (__BORLANDC__)
  842. #pragma warn -par
  843. #endif
  844. int FAR PASCAL LibMain( HANDLE hInst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine )
  845. {
  846. /*
  847. **  Unlock the data segment, that LocalInit() locks
  848. **  (LocalInit() is called from the assembler routine LibEntry()
  849. */
  850.   if( cbHeapSize != 0 )
  851.     UnlockData( 0 );
  852.  
  853.   return( TRUE );           /* Return success */
  854. }
  855. #if defined (__BORLANDC__)
  856. #pragma warn .par
  857. #endif
  858.  
  859. /*
  860. **  WEP
  861. **
  862. **  Called by Windows when DLL is unloaded
  863. **
  864. **      Parameters:   int nParameter    - Reason to unload DLL
  865. **
  866. **         Returns:   TRUE
  867. */
  868. #if defined (__BORLANDC__)
  869. #pragma warn -par
  870. #endif
  871. int _JAMFAR _PASCAL _EXPORT WEP( int nParameter )
  872. {
  873.   return( TRUE );
  874. }
  875. #if defined (__BORLANDC__)
  876. #pragma warn .par
  877. #endif
  878.  
  879. #endif /* #if defined(_WINDOWS) */
  880.  
  881. /* end of file "jamsys.c" */
  882.